package com.lin.controller.user.musicstand;


import com.lin.common.exception.BaseException;
import com.lin.common.result.Result;
import com.lin.entity.MusicData;
import com.lin.entity.vo.MusicVO;
import com.lin.service.MusicDataService;
import com.lin.service.MusicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Set;


@RestController
@RequestMapping("/music/data")
@Api(tags = "乐谱点赞等相关接口")
@Slf4j
public class MusicDataController {


    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MusicDataService musicDataService;

    @Autowired
    private MusicService musicService;
    //乐谱点赞idKey常量
    private static final String PRAISE_THUMBS_KEY = "music.thumbs.id.list.key";
    private static final String PRAISE_COLLECTION_KEY = "music.collection.id.list.key";
    private static final String PRAISE_FORWARD_KEY = "music.forward.id.list.key";
    /**
     * 点赞收藏转发
     * @param musicData
     * @return
     */
    @PostMapping("/sql")
    @ApiOperation("点赞收藏转发添加数据库0~2")
    public Result setMusicData(MusicData musicData){
        log.info("点赞收藏转发添加数据库 : {}",musicData);
        musicDataService.setMusic(musicData);
        return Result.success();
    }

    /**
     * 点赞收藏转发
     * @param musicData
     * @return
     */
    @PostMapping("/redis")
    @ApiOperation("点赞收藏转发(redis)")
    public Result setMusicThuRedis(@RequestBody MusicData musicData){
        log.info("点赞收藏转发(redis) {}",musicData);
        if(musicData.getType() == 0){
        redisTemplate.opsForSet().add(PRAISE_THUMBS_KEY,String.valueOf(musicData.getMusicId()));
        redisTemplate.opsForSet().add(String.valueOf(musicData.getMusicId()),String.valueOf(musicData.getUserId()));
        }
        if(musicData.getType() == 1){
            redisTemplate.opsForSet().add(PRAISE_COLLECTION_KEY,String.valueOf(musicData.getMusicId()));
            redisTemplate.opsForSet().add(String.valueOf(musicData.getMusicId()),String.valueOf(musicData.getUserId()));
        }
        if(musicData.getType() == 2){
            redisTemplate.opsForSet().add(PRAISE_FORWARD_KEY,String.valueOf(musicData.getMusicId()));
            redisTemplate.opsForSet().add(String.valueOf(musicData.getMusicId()),String.valueOf(musicData.getUserId()));
        }
        return Result.success();
    }

    /**
     * 取消点赞收藏
     * @param musicData
     * @return
     */
    @DeleteMapping("/remove")
    @ApiOperation("取消点赞收藏")
    public Result remove(@RequestBody MusicData musicData){
        log.info("取消点赞收藏 : {}",musicData);
        Set<String> musicIds = null;
        if(musicData.getType() == 0) {
            musicIds = redisTemplate.opsForSet().members(PRAISE_THUMBS_KEY);
        }
        else if (musicData.getType() == 1) {
            musicIds = redisTemplate.opsForSet().members(PRAISE_COLLECTION_KEY);
        }
        if (!CollectionUtils.isEmpty(musicIds)) {
            for (String musicId : musicIds) {
                Set<String> userIds = redisTemplate.opsForSet().members(String.valueOf(musicId));
                for (String userId : userIds) {
                    if (Integer.valueOf(userId) == musicData.getUserId()) {
                        redisTemplate.opsForSet().remove(String.valueOf(musicId), userId);
                    }

                }
            }
        }else if(musicData.getType() == 0){
            if(musicDataService.delete(musicData)){
            MusicVO musicVO = musicService.getMusicDetail(musicData.getMusicId());
            if(musicVO == null){
                throw new BaseException("未找到对应的乐谱");
            }
            musicVO.setThumbs(musicVO.getThumbs() - 1);
            musicService.updateMusic(musicVO);
            }else {
                throw new BaseException("未找到对应的点赞用户");
            }
        } else if (musicData.getType() == 1) {
            if(musicDataService.delete(musicData)) {
                MusicVO musicVO = musicService.getMusicDetail(musicData.getMusicId());
                if (musicVO == null) {
                    throw new BaseException("未找到对应的乐谱");
                }
                musicVO.setCollection(musicVO.getCollection() - 1);
                musicService.updateMusic(musicVO);
            }else {
                throw new BaseException("未找到对应的收藏用户");
            }
        }
        return Result.success();
    }
}
